package com.happy3w.ssp.schoolmatehome.controller;

import com.happy3w.ssp.schoolmatehome.entity.ClockInRecord;
import com.happy3w.ssp.schoolmatehome.interceptor.AttributeKeys;
import com.happy3w.ssp.schoolmatehome.service.auth.TheUserDetail;
import com.happy3w.ssp.schoolmatehome.service.record.ClockInRecordService;
import com.happy3w.ssp.schoolmatehome.service.record.ClockInRecordSummary;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/clock-in-records")
public class ClockInRecordController {
    private final ClockInRecordService clockInRecordService;

    @PostMapping
    public ResponseEntity<ClockInRecord> create(@RequestBody ClockInRecord ciRecord,
                                                @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        ClockInRecord createdRecord = clockInRecordService.create(ciRecord, userDetail.getUser().getWxUnionId());
        return ResponseEntity.ok(createdRecord);
    }

    @GetMapping
    public ResponseEntity<List<ClockInRecordSummary>> findAll(@RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        List<ClockInRecordSummary> records = clockInRecordService.findAllByOwner(userDetail.getUser().getWxUnionId());
        return ResponseEntity.ok(records);
    }

    @GetMapping("/{id}")
    public ResponseEntity<ClockInRecord> findById(@PathVariable Long id,
                                                  @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        Optional<ClockInRecord> recordOpt = clockInRecordService.findById(id, userDetail.getUser().getWxUnionId());
        return recordOpt.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    @PostMapping("/{id}")
    public ResponseEntity<ClockInRecord> update(@PathVariable Long id,
                                                @RequestBody ClockInRecord ciRecord,
                                                @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        ClockInRecord updatedRecord = clockInRecordService.update(id, ciRecord, userDetail.getUser().getWxUnionId());
        return ResponseEntity.ok(updatedRecord);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id,
                                       @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        clockInRecordService.delete(id, userDetail.getUser().getWxUnionId());
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{id}/export-preview")
    public ResponseEntity<byte[]> exportPreview(@PathVariable Long id,
                                                @RequestParam(required = false, defaultValue = "300") int width,
                                                @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        Optional<byte[]> recordOpt = clockInRecordService.exportPreview(id, width, userDetail.getUser().getWxUnionId());
        return recordOpt.map(theRecord -> {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG);
            return ResponseEntity.ok()
                    .headers(headers)
                    .body(theRecord);
        }).orElseGet(() -> ResponseEntity.notFound().build());
    }
}